//
//  AMNetworkMngTool.swift
//  amicool
//
//  Created by mario-yc on 2017/6/12.
//  Copyright © 2017年 mario-yc. All rights reserved.
//

import Foundation
let AM_LOGIN = "http://amicool.neusoft.edu.cn/api.php/login"
let AM_LIST = "http://amicool.neusoft.edu.cn/api.php/lists"

typealias NetworkBlock = (_ flag:String)->Void
typealias NetworkVideoListBlock = (_ videoList:NSArray?)->Void

class AMNetworkMngTool:NSObject{
    static var shared = AMNetworkMngTool()//单例
    
    static var loginReturnModel:LoginReturnJson? = LoginReturnJson()
    //登录请求
    func AMNetwork_Login(_ parameters:NSDictionary,block:NetworkBlock?){
        let paraArray = NSMutableArray()
        for (key,value) in parameters{
            let str = "\(key)=\(value)"
            paraArray.add(str)
        }
        let body = paraArray.componentsJoined(by: "&")
        let url = URL(string:AM_LOGIN)
        var request = URLRequest(url:url!)
        request.httpMethod = "post"
        request.httpBody = body.data(using: String.Encoding.utf8)
        let dataTask = URLSession.shared.dataTask(with: request,completionHandler:{(data, response, error) in
            if data != nil{
                if let json = try? JSONSerialization.jsonObject(with: data!, options: .mutableContainers){
                    AMNetworkMngTool.loginReturnModel = LoginReturnJson.LoginReturnJsonWithDict(json as?NSDictionary)
                    if AMNetworkMngTool.loginReturnModel?.error == nil{
                        block!("1")
                    }
                    else{
                        block!("0")
                    }
                }
                
            }
        })
        dataTask.resume()
    }
    
    func AMNetwork_GetVideoList(_ _parameters:NSDictionary,block:@escaping NetworkVideoListBlock){
        let paraArray = NSMutableArray()
        for(key,value) in _parameters{
            let str = "\(key)=\(value)"
            paraArray.add(str)
        }
        let body = paraArray.componentsJoined(by: "&")
        let url = URL(string :AM_LIST)
        var request = URLRequest(url:url!)
        request.httpMethod = "post"
        request.httpBody = body.data(using: String.Encoding.utf8)
        
        let dataTask = URLSession.shared.dataTask(with: request,completionHandler:{(data, response, error) in
            if data != nil{
                if let json = try? JSONSerialization.jsonObject(with: data!, options: .mutableContainers){
                    var videoList = [AnyObject]()
                    for video in (json as! NSArray){
                        let model = VideoReturnJson.VideoReturnJsonWithDict(video as? NSDictionary)
                        videoList.append(model!)
                    }
                    
                    block(videoList as NSArray)
                }
                else{
                    block(nil)
                }
            }
            else{
                block(nil)
            }
        })
        dataTask.resume()
    }
}
